{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>candle_begin_time</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-24 16:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>1.97e-04</td>\n",
       "      <td>1.97e-04</td>\n",
       "      <td>3.01e-05</td>\n",
       "      <td>1.35e-04</td>\n",
       "      <td>27949.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-24 17:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.90e-05</td>\n",
       "      <td>9.99e-05</td>\n",
       "      <td>4.70e-05</td>\n",
       "      <td>5.60e-05</td>\n",
       "      <td>51104.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-24 18:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>6.30e-05</td>\n",
       "      <td>7.47e-05</td>\n",
       "      <td>5.01e-05</td>\n",
       "      <td>7.18e-05</td>\n",
       "      <td>106084.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-24 19:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.17e-05</td>\n",
       "      <td>7.33e-05</td>\n",
       "      <td>6.11e-05</td>\n",
       "      <td>7.04e-05</td>\n",
       "      <td>60643.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-24 20:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.00e-05</td>\n",
       "      <td>7.70e-05</td>\n",
       "      <td>6.10e-05</td>\n",
       "      <td>6.10e-05</td>\n",
       "      <td>26093.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2145</th>\n",
       "      <td>2018-01-24 19:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.60e+00</td>\n",
       "      <td>1.62e+00</td>\n",
       "      <td>1.58e+00</td>\n",
       "      <td>1.62e+00</td>\n",
       "      <td>8728.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2146</th>\n",
       "      <td>2018-01-24 20:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.61e+00</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>1.61e+00</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>45549.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2147</th>\n",
       "      <td>2018-01-24 21:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.70e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.67e+00</td>\n",
       "      <td>95792.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2148</th>\n",
       "      <td>2018-01-24 22:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1.64e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>21623.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2149</th>\n",
       "      <td>2018-01-24 23:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1202.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2150 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        candle_begin_time  symbol      open      high       low     close     volume\n",
       "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
       "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
       "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
       "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
       "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
       "...                   ...     ...       ...       ...       ...       ...        ...\n",
       "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
       "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
       "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
       "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
       "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
       "\n",
       "[2150 rows x 7 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 对print出的数据格式进行修正\n",
    "pd.set_option('expand_frame_repr', False)  # 当列太多时不换行\n",
    "pd.set_option('precision', 2)  # 浮点数的精度\n",
    "pd.set_option('display.max_rows', 10) # 显示的最大行数\n",
    "\n",
    "# =====导入数据\n",
    "df = pd.read_csv(\n",
    "    r'C:\\notebooks\\quantitative_trading_notes\\data\\BITFINEX-1H-data-20180124.csv',\n",
    "    skiprows=1,\n",
    "    # nrows=15,\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         close  收盘价_3天均值\n",
      "0     1.35e-04       NaN\n",
      "1     5.60e-05       NaN\n",
      "2     7.18e-05  8.75e-05\n",
      "3     7.04e-05  6.61e-05\n",
      "4     6.10e-05  6.77e-05\n",
      "...        ...       ...\n",
      "2145  1.62e+00  1.61e+00\n",
      "2146  1.66e+00  1.62e+00\n",
      "2147  1.67e+00  1.65e+00\n",
      "2148  1.65e+00  1.66e+00\n",
      "2149  1.68e+00  1.67e+00\n",
      "\n",
      "[2150 rows x 2 columns]\n"
     ]
    }
   ],
   "source": [
    "# 如何得到每一天的最近3天close的均值呢？即如何计算常用的移动平均线？\n",
    "# 使用rolling函数\n",
    "df['收盘价_3天均值'] = df['close'].rolling(3).mean()\n",
    "print(df[['close', '收盘价_3天均值']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0            NaN\n",
      "1            NaN\n",
      "2            NaN\n",
      "3            NaN\n",
      "4       1.35e-04\n",
      "          ...   \n",
      "2145    1.62e+00\n",
      "2146    1.66e+00\n",
      "2147    1.67e+00\n",
      "2148    1.67e+00\n",
      "2149    1.68e+00\n",
      "Name: close, Length: 2150, dtype: float64\n",
      "0            NaN\n",
      "1            NaN\n",
      "2       5.60e-05\n",
      "3       5.60e-05\n",
      "4       6.10e-05\n",
      "          ...   \n",
      "2145    1.58e+00\n",
      "2146    1.58e+00\n",
      "2147    1.62e+00\n",
      "2148    1.65e+00\n",
      "2149    1.65e+00\n",
      "Name: close, Length: 2150, dtype: float64\n",
      "0            NaN\n",
      "1            NaN\n",
      "2       4.16e-05\n",
      "3       8.74e-06\n",
      "4       5.87e-06\n",
      "          ...   \n",
      "2145    2.38e-02\n",
      "2146    3.74e-02\n",
      "2147    2.25e-02\n",
      "2148    9.06e-03\n",
      "2149    1.42e-02\n",
      "Name: close, Length: 2150, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# rolling(n)即为取最近n行数据的意思，只计算这n行数据。后面可以接各类计算函数，例如max、min、std等\n",
    "print(df['close'].rolling(5).max())\n",
    "print(df['close'].rolling(3).min())\n",
    "print(df['close'].rolling(3).std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         close  收盘价_至今均值\n",
      "0     1.35e-04  1.35e-04\n",
      "1     5.60e-05  9.54e-05\n",
      "2     7.18e-05  8.75e-05\n",
      "3     7.04e-05  8.32e-05\n",
      "4     6.10e-05  7.88e-05\n",
      "...        ...       ...\n",
      "2145  1.62e+00  2.76e+02\n",
      "2146  1.66e+00  2.76e+02\n",
      "2147  1.67e+00  2.76e+02\n",
      "2148  1.65e+00  2.76e+02\n",
      "2149  1.68e+00  2.76e+02\n",
      "\n",
      "[2150 rows x 2 columns]\n"
     ]
    }
   ],
   "source": [
    "# rolling可以计算每天的最近3天的均值，如果想计算每天的从一开始至今的均值，应该如何计算？\n",
    "# 使用expanding操作\n",
    "df['收盘价_至今均值'] = df['close'].expanding().mean()\n",
    "print(df[['close', '收盘价_至今均值']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1.35e-04\n",
      "1       1.35e-04\n",
      "2       1.35e-04\n",
      "3       1.35e-04\n",
      "4       1.35e-04\n",
      "          ...   \n",
      "2145    1.14e+04\n",
      "2146    1.14e+04\n",
      "2147    1.14e+04\n",
      "2148    1.14e+04\n",
      "2149    1.14e+04\n",
      "Name: close, Length: 2150, dtype: float64\n",
      "0       1.35e-04\n",
      "1       5.60e-05\n",
      "2       5.60e-05\n",
      "3       5.60e-05\n",
      "4       5.60e-05\n",
      "          ...   \n",
      "2145    6.25e-06\n",
      "2146    6.25e-06\n",
      "2147    6.25e-06\n",
      "2148    6.25e-06\n",
      "2149    6.25e-06\n",
      "Name: close, Length: 2150, dtype: float64\n",
      "0            NaN\n",
      "1       5.56e-05\n",
      "2       4.16e-05\n",
      "3       3.51e-05\n",
      "4       3.19e-05\n",
      "          ...   \n",
      "2145    1.49e+03\n",
      "2146    1.49e+03\n",
      "2147    1.49e+03\n",
      "2148    1.49e+03\n",
      "2149    1.49e+03\n",
      "Name: close, Length: 2150, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# expanding即为取从头至今的数据。后面可以接各类计算函数\n",
    "print(df['close'].expanding().max())\n",
    "print(df['close'].expanding().min())\n",
    "print(df['close'].expanding().std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        candle_begin_time  symbol      open      high       low     close     volume  收盘价_3天均值  收盘价_至今均值\n",
      "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99       NaN  1.35e-04\n",
      "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00       NaN  9.54e-05\n",
      "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15  8.75e-05  8.75e-05\n",
      "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77  6.61e-05  8.32e-05\n",
      "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50  6.77e-05  7.88e-05\n",
      "...                   ...     ...       ...       ...       ...       ...        ...       ...       ...\n",
      "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98  1.61e+00  2.76e+02\n",
      "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64  1.62e+00  2.76e+02\n",
      "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54  1.65e+00  2.76e+02\n",
      "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11  1.66e+00  2.76e+02\n",
      "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25  1.67e+00  2.76e+02\n",
      "\n",
      "[2150 rows x 9 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df)\n",
    "df.to_csv('output.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
